<?php 
/*** Create By fu */
namespace app\api\validate;

use think\Validate;
use think\Request;
use think\Exception;
use app\lib\exception\ParameterException;

/**
* 
*/
class BaseValidate extends Validate
{
	
	public function goCheck()
	{
		//获取http传入的参数
		//对这些参数进行校验
		$request = Request::instance();
		$params = $request->param();

		$result = $this->batch()->check($params);
		if(!$result){
			//使用自定义异常处理方法begin
			throw new ParameterException(['msg'=> $this->error,]);
			
		}else{
			return true;
		}
	}

	//验证结果 return 给 error 属性
	protected function isPositiveInteger($value,$rule='',$data='',$field='')
	{
		if(is_numeric($value) && is_int($value+0) && ($value+0) >0){
			return true;
		}else{
			return false;
		}
	}

	//判断有值且不为空
	protected function isNotEmpty($value,$rule='',$data='',$field='')
	{
		if(empty($value)){
			return false;
		}else{
			return true;
		}
	}
	//过滤掉不需要的参数 
	public function getDataByRule($arrays)
	{
		//如果传过来的参数包含 user_id 和uid 将当做恶意请求
		if(array_key_exists('user_id', $arrays) | array_key_exists('uid', $arrays))
		{
			throw new ParameterException(['msg'=>'参数包含非法参数名user_id或uid']);
		}

		$newArray = [];
		foreach ($this->rule as $key => $value) {
			$newArray[$key] = $arrays[$key];
		}

		return $newArray;
	}

	//判断电话
	public function isMobile($value)
	{
		$rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
		$result = preg_match($rule, $value);

		return $result ? true : false;
	}
}











 ?>